#include "../include/log.h"

#include <unistd.h>
#include <stdlib.h>

char tty_dev[100];
char ppp_path[100];
char ppp_apn[100];
char ppp_username[100];
char ppp_password[100];
char ppp_tel[100];
int check_flg=0;    //loop call pppd
int log_flg=0;      //0: close log


void ShowHelp( char *argv[])
{
    //export DC_ROOT=$SHELL_FOLDER
    // export APN="internet"
    // export PPP_TEL="*99***1#"
    // #export PPP_TEL="*98*1#"
    // export PPP_USER="   "
    // export PPP_PASSWORD="   "
    printf("usage: %s [-a APN][-T telephone][-U username][-P password][-D ttydev][-m ppppath][-C ][-S ]\n",argv[0]);
    printf("\t\t:-C means if you kill pppd, it won't restart\n");
    printf("\t\t:default config is: APN=internet TEL=*99***1# others is NULL \n");
}

char *get_exe_path(char *buf, int count)
{
    int i;
    int rslt = readlink("/proc/self/exe", buf, count - 1);
    if (rslt < 0 || (rslt >= count - 1))
    {
        return NULL;
    }
    buf[rslt] = '\0';
    for (i = rslt; i >= 0; i--)
    {
        if (buf[i] == '/')
        {
            buf[i + 1] = '\0';
            break;
        }
    }
    return buf;
}


void GetConfig(int argc,  char  *argv[])
{
    int  opt;
    while ((opt = getopt(argc, argv, "cCsSa:A:t:T:u:U:p:P:d:D:m:M:")) != -1) {
        switch (opt) {
        case 'C':
        case 'c':
            check_flg = 1;
            break;
        case 'S':
        case 's':
            log_flg = 1;
            break;   
        case 'a':
        case 'A':
            snprintf(ppp_apn,sizeof(ppp_apn)-1,"%s",optarg);
            break;
        case 'd':
        case 'D':
            snprintf(tty_dev,sizeof(tty_dev)-1,"%s",optarg);
            break;    
        case 't':
        case 'T':
            snprintf(ppp_tel,sizeof(ppp_tel)-1,"%s",optarg);
            break;
        case 'u':
        case 'U':
            snprintf(ppp_username,sizeof(ppp_username)-1,"%s",optarg);
            break;   
        case 'p':
        case 'P':
            snprintf(ppp_password,sizeof(ppp_password)-1,"%s",optarg);
            break; 
        case 'm':
        case 'M':
            snprintf(ppp_path,sizeof(ppp_path)-1,"%s",optarg);
            break;                

        default: /* '?' */
            break;
        }
    }

    printf("APN      =%s\n",ppp_apn);
    printf("USER_NAME=%s\n",ppp_username);
    printf("PASSWORD =%s\n",ppp_password);
    printf("TELEPHONE=%s\n",ppp_tel);
    printf("TTY_DEV  =%s\n",tty_dev);

    if(ppp_path[0]==0)
    {
        char buf[sizeof(ppp_path)-20];
        printf("%s\n", get_exe_path(buf, sizeof(buf)-1));
        snprintf(ppp_path, sizeof(ppp_path), "%s%s",buf,"ppp/ppp-on");
    }
    if(ppp_apn[0]==0)
        snprintf(ppp_apn, sizeof(ppp_apn), "%s","\" \"");
    if(ppp_username[0]==0)
        snprintf(ppp_username, sizeof(ppp_username), "%s","\" \"");
    if(ppp_password[0]==0)
        snprintf(ppp_password, sizeof(ppp_password), "%s","\" \"");
    if(ppp_tel[0]==0)
        snprintf(ppp_tel, sizeof(ppp_tel), "%s","\" \"");





}

char* ShowEth(int ret)
{
    switch (ret)
    {
    case 0:
        return "DEVICE_NONE";
        break;
    case -1:
        return "ioctl SIOCGIFFLAGS error";
        break;    
    case 1:
        return "DEVICE_DOWN";
        break; 
    case 2:
        return "DEVICE_UNPLUGGED";
        break; 
    case 3:
        return "Linked";
        break;
    default:
        return "Other Err";
        break;
    }
}


extern int pppThreadRunner(int loop);
int main(int argc,  char  *argv[])
{
    LOGInit();
    LOG_MESSAGE_RAW(DEBUG,"Start Gprs Moudle Tets");
    ShowHelp(argv);
    GetConfig(argc,argv);

    int ret= pppThreadRunner(0);
    printf("[%d] : %s\n",ret,ShowEth(ret));
    return 0;
}


